<?php
require_once "PHPUnit/Framework.php";

// dummy definition for add_filter function that all plugins call
function add_filter($tag, $function_to_add, $priority = 10)
{
}

include("urlizer.php");

class UrlizeTest extends PHPUnit_Framework_TestCase 
{

    function testUrlize() 
    {
        // test normal case
        $this->assertEquals(urlize("http://www.supermind.org"), 
          "<a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

        // test more than one URL
        $this->assertEquals(urlize("http://www.supermind.org http://www.google.com"), 
          "<a href=\"http://www.supermind.org\">http://www.supermind.org</a>  <a href=\"http://www.google.com\">http://www.google.com</a>");

        // test with text
        $this->assertEquals(urlize("This is a test http://www.supermind.org"), 
          "This is a test <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

        // test existing links not changed
        $this->assertEquals(urlize("This is another test <a href=\"http://www.supermind.org\">http://www.supermind.org</a>"), 
          "This is another test <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

        $this->assertEquals(urlize("This is another test <a href=\"http://www.supermind.org\">foo</a>"), 
          "This is another test <a href=\"http://www.supermind.org\">foo</a>");

        // its not great that it adds a link within a link, but its not the end of the world
        $this->assertEquals(urlize("This is another test <a href=\"http://www.supermind.org\">foo http://www.supermind.org bar</a>"), 
          'This is another test <a href="http://www.supermind.org">foo <a href="http://www.supermind.org">http://www.supermind.org</a>  bar</a>');
        
        // test anchor with name attribute
        $this->assertEquals(urlize("<a name=\"foo\" href=\"http://www.supermind.org\">foobar</a> http://www.supermind.org"), 
        "<a name=\"foo\" href=\"http://www.supermind.org\">foobar</a> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");
      
        // test leading HTML tag (without space)
/*
        $this->assertEquals(urlize("<br/>http://www.supermind.org"), 
          "<br/><a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

      // test trailing HTML tag (without space)
        $this->assertEquals(urlize("http://www.supermind.org<br/>"), 
          "<a href=\"http://www.supermind.org\">http://www.supermind.org</a><br/>");

*/

        // test leading HTML tag
        $this->assertEquals(urlize("<br/> http://www.supermind.org"), 
          "<br/> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

  
        // test new line
        $this->assertEquals(urlize("\n\nhttp://www.supermind.org"), 
          "<a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

        // test new line, trailing and leading HTML tags
        $this->assertEquals(urlize("\n<p><br> http://www.supermind.org <br>"), 
          "<p><br> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>  <br>");

        // test case-sensitivity
        $this->assertEquals(urlize("\n<P><BR> http://www.supermind.org <br>"), 
          "<P><BR> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>  <br>");

        // ensure only unanchored URL is changed
        $this->assertEquals(urlize("<a href=\"http://www.supermind.org\">foobar</a> http://www.supermind.org"), 
          "<a href=\"http://www.supermind.org\">foobar</a> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

        // test case-sensitivity of anchor tag
        $this->assertEquals(urlize("<A HREF=\"http://www.supermind.org\">foobar</A> http://www.supermind.org"), 
          "<A HREF=\"http://www.supermind.org\">foobar</A> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

        // test case-sensitivity of URL
        $this->assertEquals(urlize("<a href=\"http://www.supermind.org\">foobar</a> http://www.supermind.org"), 
          "<a href=\"http://www.supermind.org\">foobar</a> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");

        // TODO: test quote-less URLs
/*
        $this->assertEquals(urlize("<a href=http://www.supermind.org>foobar</a> http://www.supermind.org"), 
          "<a href=http://www.supermind.org>foobar</a> <a href=\"http://www.supermind.org\">http://www.supermind.org</a>");



        // test trailing # and exclamation
        $this->assertEquals(urlize("http://www.supermind.org/foo.html#!"), 
          "<a href=\"http://www.supermind.org/foo.html#\">http://www.supermind.org/foo.html#</a>!");

*/
        // test trailing pound
        $this->assertEquals(urlize("http://www.supermind.org/foo.html#"), 
          "<a href=\"http://www.supermind.org/foo.html#\">http://www.supermind.org/foo.html#</a>");

        $this->assertEquals(urlize("http://www.supermind.org/foo.html#bar"), 
          "<a href=\"http://www.supermind.org/foo.html#bar\">http://www.supermind.org/foo.html#bar</a>");


        // test trailing slash
        $this->assertEquals(urlize("http://www.supermind.org/"), 
          "<a href=\"http://www.supermind.org/\">http://www.supermind.org/</a>");   

/*
        // test <code>
        $this->assertEquals(urlize("<code>http://www.supermind.org/foo.html</code>"), 
          "<code>http://www.supermind.org/foo.html</code>");

        // test <code> with other text
        $this->assertEquals(urlize("<code>This is http://www.supermind.org/foo.html a test</code>"), 
          "<code>This is http://www.supermind.org/foo.html a test</code>");

        // TODO: test more than one URL in <code>
        $this->assertEquals(urlize("<code>http://www.supermind.org/foo.html http://www.supermind.org/foo.html</code>"), 
          "<code>Test http://www.supermind.org/foo.html http://www.supermind.org/foo.html</code>");
*/            
    }

    function testUrlizeSrc() {
        // test src
        $this->assertEquals(urlize("This is an img test <img src=\"http://www.supermind.org\">"), 
          "This is an img test <img src=\"http://www.supermind.org\">");
        
        $this->assertEquals(urlize("This is a script test <script src=\"http://www.supermind.org\">"), 
          "This is a script test <script src=\"http://www.supermind.org\">");
    }

    function testUrlizeProtocols() {
        // test src
        $this->assertEquals(urlize("https://www.supermind.org"), 
          "<a href=\"https://www.supermind.org\">https://www.supermind.org</a>");
        
        $this->assertEquals(urlize("ftp://www.supermind.org"), 
          "<a href=\"ftp://www.supermind.org\">ftp://www.supermind.org</a>");
    }

    function testUrlizePunctuation() {
        // test trailing dot.
        $this->assertEquals(urlize("Go to http://www.supermind.org."), 
          "Go to <a href=\"http://www.supermind.org\">http://www.supermind.org</a>.");

        // test question mark
        $this->assertEquals(urlize("Go to http://www.supermind.org?"), 
          "Go to <a href=\"http://www.supermind.org\">http://www.supermind.org</a>?");

        // test trailing comma.
        $this->assertEquals(urlize("Go to http://www.supermind.org, then leave."), 
          "Go to <a href=\"http://www.supermind.org\">http://www.supermind.org</a>,  then leave.");

        // test trailing exclamation
        $this->assertEquals(urlize("Go to http://www.supermind.org!"), 
          "Go to <a href=\"http://www.supermind.org\">http://www.supermind.org</a>!");
    }
}

?>
